package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论模块")
@Slf4j
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 1 发表评论功能
     * 接口地址: /v1/comment/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @ApiOperation("发表评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        log.debug("commentDTO = " + commentDTO);
        /*
            1.获取信息
            2.校验登陆状态
                2.1 若未登录 返回2
                2.2 若已登录 INSERT -> 返回1
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    /**
     * 2 获取评论功能
     * 接口地址: /v1/comment/selectByWeiboId
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: List<CommentVO>
     */
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "long")
    @ApiOperation("获取评论")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Long id){
        /*
            1.获取用户数据
            2.查询数据库对应信息
            3.返回数据集合
         */
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOS);
    }
}
